<?php
// $fileInfo=$_FILES['myfile'];

function fileupload($fileInfo,$maxSize = 2097152,$path = 'up',$flag=true,
$allowExt = array('jpeg','jpg','png','gif','wemp'))
{
    if($fileInfo['error']>0)
    {
        switch($fileInfo['error']){
            case 1:
                $mes = "上传文件超过了PHP配置upload_max_filessize的值";
                break;
            case 2:
                $mes = "超过了表单max_file_size限制的大小";
                break;
            case 3:
                $mes  ="文件部分被上传";
                break;
            case 4:
                $mes  = "没有选择上传文件";
                break;
            case 6:
                $mes  = "没有找到 临时目录";
                break;
            case 7:
            case 8:
                $mes  = "系统错误";
                break;
        }
        exit($mes);
    }
    
    //检测上传文件的类型
    $ext=pathinfo($fileInfo['name'],PATHINFO_EXTENSION);
    if(!in_array($ext,$allowExt))
    {
        exit("非法文件类型");
    }
    
    //文件上传大小是否符合规范
    
    if($fileInfo['size']>$maxSize)
    {
        exit('上传文件过大');
    }
    
     //检测是否为真实图片类型
     if($flag){
        if(!getimagesize($fileInfo['tmp_name']))
        {
            exit('不是真正的图片类型');
        }
     }
     
    //判断文件上传是否是http post
    if(!is_uploaded_file($fileInfo['tmp_name']))
    {
        exit('文件不是通过http post方式上传的');
    }
    
    //$path = 'up';
    $umiName = md5(uniqid(microtime(true),true)).'.'.$ext;
    $destination=$path.'/'.$umiName;
    if(!file_exists($path))
    {
        mkdir($path,0777,true);
        chmod($path,0777);
    }
    if(@move_uploaded_file($fileInfo['tmp_name'],$destination))
    {
        echo '文件上传成功';
    }
    // return array(
    //     'newName' =>$destination,
    //     'size' =>$fileInfo['size'],
    //     'type'=>$fileInfo['type']
    // );
    return $destination;
}